class GameUI extends eui.Component {
	private lb_score: eui.Label;//得分
	private lb_scoreTwo: eui.Label;//得分
	private lb_gameoverGroup: eui.Group;//结束组
	private lb_bestscore: eui.Label;//最好成绩
	private lb_restPlay: eui.Button;//重新开始
	private lb_start: eui.Label;//开始按钮
	private lb_gameStart: eui.Group;//开始组
	private lb_reward: eui.Group;//	奖励组
	private lb_reward_btn: eui.Image;//	领去奖励按钮
	private lb_count: eui.Label;//	倒计时
	private lb_reward_text: eui.Group;
	private lb_reward_close: eui.Image;
	private lb_reward_num: eui.Label;
	public _rePlay: boolean = false;
	public _start: boolean = false;
	public _count: boolean = false;
	public countdown: number = 10;
	public userId: number = 0;
	private _t;
	private _music: Music;
	public constructor() {
		super();
		this.skinName = "src/GameUI.exml";

		this.init();

		this.lb_restPlay.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick_replay, this);
		this.lb_start.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick_start, this);
		this.lb_reward_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick_reward, this);
		this.lb_reward_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick_Close, this);

		this._music = new Music();
		this._music.addEventListener(egret.Event.SOUND_COMPLETE, this.handleComplete, this);

	}
	private handleComplete() {
		console.log('加载完成')
	}
	private init() {
		this.Score = 0;
		this.Count = this.countdown;
		this.lb_gameoverGroup.visible = false;
		this.lb_reward.visible = false;
		this.lb_reward_text.visible = false;

		this.lb_gameStart.visible = false;

		this.Count = this.urlParam('countdown') - 0 || 10;	//	倒计时
		this.countdown = this.urlParam('countdown') - 0 || 10;	//	倒计时
		this.Score = this.urlParam('startLevel') - 0 || 0;	//	开始关数
		this.userId = this.urlParam('userID') - 0 || 0;	//	userId
	}
	public get Count(): number {
		return parseInt(this.lb_count.text);
	}
	public set Count(value: number) {
		this.lb_count.text = value.toString();
	}
	////////////计算得分//////////////////////////////////////
	public get Score(): number {
		return parseInt(this.lb_score.text);
	}
	public set Score(value: number) {
		this.lb_score.text = value.toString();
	}
	///////////计算最好成绩/////////////////////////////////
	public getBestScore(): number {
		var str = egret.localStorage.getItem("POLE_BESTSCORE");
		if (str == null) {
			return 0;
		}
		else {
			return parseInt(str);
		}
	}
	public setBestScore(value: number) {
		egret.localStorage.setItem("POLE_BESTSCORE", value.toString());
	}
	////////////游戏结束////////////////////////////////////
	public showOver() {
		// this.lb_gameoverGroup.visible = true;
		// if (this.Score > this.getBestScore()) {
		// 	this.setBestScore(this.Score);
		// }
		// this.lb_bestscore.text = this.getBestScore().toString();
		// this.lb_scoreTwo.text = this.Score.toString();

		// this._music.stopBg();
		// this._music.stopCount();
		// this._music.playGameOver();

		// const url = `http://jmp96615.oicp.net/lsk/mobile/game/gameOver?userID=${this.userId}&endLevel=${this.Score}`;
		const url = `http://zzyunxun.com:8093/lsk/mobile/game/gameOver?userID=${this.userId}&endLevel=${this.Score}`;

		// setTimeout(function () {
		window.location.replace(url)
		// }, 3000);
	}
	// 重新开始
	private onClick_replay() {
		this._rePlay = true;
		this.init();

		// this._music.stopGameOver();
		this._music.playBg();
		this._music.playCount();
	}
	// 点击开始
	public onClick_start() {
		this._start = true;
		this.lb_gameStart.visible = false;
		this.timer();

		// this._music.playGameOver();
		this._music.playBg();
	}
	// 倒计时
	public timer() {
		this._music.playCount();
		// this._music.playGameOver()
		this._t = setInterval(() => {
			if (this.Count == 0) {
				// this.showOver();
				// this._count = false;
				this.clear();
			} else {
				this._count = true;
				this.Count -= 1;
			}
		}, 1000)
	}
	// 停止倒计时
	public clear() {
		this._count = false;
		clearInterval(this._t);
		this._music.stopCount();
	}
	// 出发领奖事件
	public reward() {
		this._start = false;
		this.lb_reward.visible = true;
	}
	// 点击领奖按钮
	private onClick_reward() {
		// this._start = true;

		// this.Count = 5;	http://jmp96615.oicp.net/lsk/mobile/game/toGameIndex?userId=2
		// this.timer();

		// var url = `http://jmp96615.oicp.net/lsk/mobile/game/getReward?userID=${this.userId}`

		var url = `http://zzyunxun.com:8093/lsk/mobile/game/getReward?userID=${this.userId}`

		var request = new egret.HttpRequest();
		request.responseType = egret.HttpResponseType.TEXT;
		request.open(url, egret.HttpMethod.GET);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send();
		request.addEventListener(egret.Event.COMPLETE, this.onGetComplete, this);
		request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onGetIOError, this);
		request.addEventListener(egret.ProgressEvent.PROGRESS, this.onGetProgress, this);

	}

	private onGetComplete(event: egret.Event): void {
		var request = <egret.HttpRequest>event.currentTarget;
		console.log('get data:', request.response);
		this.lb_reward.visible = false;
		this.lb_reward_text.visible = true;
		this.lb_reward_num.text = JSON.parse(request.response).reward.toString();
	}

	private onGetIOError(event: egret.IOErrorEvent): void {
		console.log('get error : ' + event);
	}

	private onGetProgress(event: egret.ProgressEvent): void {
		console.log('get progress : ' + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
	}

	private onClick_Close() {
		this._start = true;
		this.lb_reward_text.visible = false;
		this.Count = this.countdown;
		this.timer();
	}
	public playGameOver() {
		this._music.userId = this.userId;
		this._music.score = this.Score;
		this._music.stopBg();
		this._music.stopCount();
		this._music.playGameOver();
	}// 获取url参数
	private urlParam(type) {
		var _s = window.location.search,
			attr = {};

		if (_s.indexOf("?") >= 0) {
			_s = _s.substring(1);
			var arry = _s.split('&');
			for (var i = 0; i < arry.length; i++) {
				var item = arry[i];
				attr[item.split("=")[0]] = item.split("=")[1];
			}
		}
		return attr[type];
	}
}